package com.party.core.service.member.impl;

import com.google.common.base.Strings;
import com.party.common.constant.Constant;
import com.party.common.utils.PasswordUtils;
import com.party.common.utils.PinyinUtil;
import com.party.core.exception.BusinessException;
import com.party.core.model.AuditStatus;
import com.party.core.model.YesNoStatus;
import com.party.core.model.commune.CMemberInfo;
import com.party.core.model.member.Member;
import com.party.core.service.commune.ICMemberInfoService;
import com.party.core.service.member.ICmemberBizService;
import com.party.core.service.member.IMemberService;
import com.party.core.utils.MyBeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: party
 * @description: C端用户业务服务接口实现
 * @author: Hua Qunhao
 * @create: 2019-09-10 14:47
 **/
@Service
public class CmemberBizService implements ICmemberBizService {

    @Autowired
    private IMemberService memberService;

    @Autowired
    private ICMemberInfoService memberInfoService;

    @Override
    public Member saveOrUpdateCmember(Member input_member, String partnerId) throws Exception {

        Member save_member = null;
        //用户主体
        if (StringUtils.isEmpty(input_member.getId())) {
            List<Member> members = memberService.checkUserName("mobile", input_member.getMobile(), "", Member.MEMBER_PERSONAL);
            if (members.size() > 0) {
                throw new BusinessException("手机号已存在");
            }
            //初始化member
            save_member = this.createMember(Member.MEMBER_PERSONAL);

            save_member.setLogo(input_member.getLogo());
            save_member.setJobTitle(input_member.getJobTitle());
            save_member.setMobile(input_member.getMobile());
            save_member.setCompany(input_member.getCompany());
            save_member.setCity(input_member.getCity());
            save_member.setSex(input_member.getSex());
            save_member.setRealname(PinyinUtil.filterEmoji(input_member.getRealname(), ""));
            save_member.setPinyin(PinyinUtil.hanziToPinyin(input_member.getRealname(), ""));
            save_member.setWechatNum(input_member.getWechatNum());

            memberService.update(save_member);
        } else {
            // 从数据库取出记录的值
            save_member = memberService.get(input_member.getId());
            // 将编辑表单中的非NULL值覆盖数据库记录中的值
            MyBeanUtils.copyBeanNotNull2Bean(input_member, save_member);

            String realname = input_member.getRealname();
            if (StringUtils.isNoneBlank(realname)) {
                realname = PinyinUtil.filterEmoji(realname, "");
                save_member.setRealname(realname);
                save_member.setPinyin(PinyinUtil.hanziToPinyin(realname, ""));
            }
            memberService.update(save_member);
        }
        return save_member;
    }

    public Member createMember(Integer type) {
        Member member = new Member();
        member.setUserStatus(Integer.parseInt(AuditStatus.AUDITING.getCode()));//默认未审核
        // 联合发起
        member.setIsCopartner(YesNoStatus.NO_NEW.getCode());
        // 合作商
        member.setIsPartner(YesNoStatus.NO.getCode());
        // 管理员
        member.setIsAdmin(YesNoStatus.NO.getCode());
        // 达人
        member.setIsExpert(YesNoStatus.NO.getCode());
        // 分销商
        member.setIsDistributor(YesNoStatus.NO.getCode());
        // 合作渠道
        member.setIsCosponsor(YesNoStatus.NO.getCode());
        member.setPicNum(0);
        member.setIsOpen(YesNoStatus.NO.getCode());
        member.setPassword(PasswordUtils.encryptPassword(Constant.DEFALT_PASSWORD));
        member.setType(type);
        memberService.insert(member);//保存会员

        return member;
    }

    /**
     * 如果当前用户信息为空 更新当前用户的信息
     *
     * @param member
     * @param curMember
     */
    public Member updateImportantInfo(Member member, Member curMember) {
        //所有用户自主填写的内容都需过滤Emoji表情
        //昵称
        if (!Strings.isNullOrEmpty(member.getRealname()) && Strings.isNullOrEmpty(curMember.getRealname())) {
            curMember.setRealname(PinyinUtil.filterEmoji(member.getRealname(), ""));
            curMember.setPinyin(PinyinUtil.hanziToPinyin(member.getRealname(), ""));
        }
        //公司
        if (!Strings.isNullOrEmpty(member.getCompany()) && Strings.isNullOrEmpty(curMember.getCompany())) {
            curMember.setCompany(PinyinUtil.filterEmoji(member.getCompany(), ""));
        }
        //职位
        if (!Strings.isNullOrEmpty(member.getJobTitle()) && Strings.isNullOrEmpty(curMember.getJobTitle())) {
            curMember.setJobTitle(PinyinUtil.filterEmoji(member.getJobTitle(), ""));
        }
        //微信号
        if (!Strings.isNullOrEmpty(member.getWechatNum()) && Strings.isNullOrEmpty(curMember.getWechatNum())) {
            curMember.setWechatNum(PinyinUtil.filterEmoji(member.getWechatNum(), ""));
        }
        //行业
        if (!Strings.isNullOrEmpty(member.getIndustry()) && Strings.isNullOrEmpty(curMember.getIndustry())) {
            curMember.setIndustry(member.getIndustry());
        }
        //城市
        if (!Strings.isNullOrEmpty(member.getCity()) && Strings.isNullOrEmpty(curMember.getCity())) {
            curMember.setCity(member.getCity());
        }
        if (!Strings.isNullOrEmpty(member.getFullname()) && Strings.isNullOrEmpty(curMember.getFullname())) {
            curMember.setFullname(member.getFullname());
        }
        // 性别
        if (null != member.getSex()) {
            curMember.setSex(member.getSex());
        }
        if (null != member.getCountryCode()) {
            curMember.setCountryCode(member.getCountryCode());
        }
        memberService.update(curMember);
        return curMember;
    }

    /**
     * @Description: 判断当前用户是否审核通过
     * @Param:
     * @return:
     * @Author: Hua Qunhao
     * @Date: 2019/10/29
     */
    public boolean isCheck(String memberId) {
        boolean br = false;
        CMemberInfo byMemberId = memberInfoService.getByMemberId(memberId);
        if (null != byMemberId && CMemberInfo.CHECK_STATUS_ADOPT == byMemberId.getCheckStatus()) {
            br = true;
        }
        return br;
    }
}
